package com.dz.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dz.reggie.common.R;
import com.dz.reggie.dto.SetmealDto;
import com.dz.reggie.entity.Setmeal;
import com.dz.reggie.entity.SetmealDish;
import com.dz.reggie.service.SetmealDishService;
import com.dz.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    public R<Page<Setmeal>> page(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //StringUtils.isNotEmpty(name)?
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }


    // todo - 修改菜品功能

    /**
     * 前端用户界面，展示套餐中的菜品数据
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> querySetmeals(Setmeal setmeal){
        // 查询套餐内菜品
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        setmealWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(setmealWrapper);

        return R.success(list);
    }

    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("save方法中：{}",setmealDto);

        setmealService.saveWithDishes(setmealDto);
        return R.success("新增套餐信息成功");
    }

    /**
     * 删除，也需要操作两张表：Setmeal和Setmeal_Dish
     * 先遍历Setmeal_Dish表按套餐id删除关联的菜品，再删除套餐
     * @param idList
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)  // 清理setMealCache这个缓存下的所有数据
    public R<String> delete(@RequestParam("ids") ArrayList<Long> idList){
        log.info("idList={}", idList);
        // todo - 遍历List，精确清理分类下的菜品缓存数据
        // for (Long id : idList){
        //     // Long categoryId = dishService.getById(id).getCategoryId();
        //     // String key = "dish_" + categoryId + "_1";
        //     // redisTemplate.delete(key);
        // }
        setmealService.deleteWithDishes(idList);

        return R.success("删除成功！");
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){

        setmealService.updateWithDishes(setmealDto);
        return R.success("修改成功！");
    }


    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        log.info("id={}", id);
        SetmealDto setmealDto = setmealService.getWithDishes(id);
        return R.success(setmealDto);
    }


}
